{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "757b49b4",
   "metadata": {},
   "source": [
    "# Logging and Debugging"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d36e19",
   "metadata": {},
   "source": [
    "<h2>Enable logging in a Python script</h2>\n",
    "\n",
    "1. Import the debug_mode script\n",
    "   ```sh\n",
    "   from interpret.develop import debug_mode\n",
    "   ```\n",
    "\n",
    "2. Add the following line of code to the script/test you are running to enable logging using the debug build of the native library\n",
    "\n",
    "   ```sh\n",
    "   debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)\n",
    "   ```\n",
    "\n",
    "   * In the above command example logs are sent to _log.txt_ file and the INFO logging level is used\n",
    "   * Note that the C++ project should be compiled in DEBUG mode\n",
    "\n",
    "3. stdout/stderr can also be used as output, e.g.\n",
    "\n",
    "   ```sh\n",
    "   debug_mode(log_filename=stdout, log_level='INFO', native_debug=True)\n",
    "   ```\n",
    "\n",
    "   * Note that by default `pytest` captures output sent to `stdout` and `stderr` so you do not see it when running tests. If you want to see the output add the following parameter in `.vscode\\settings.json`\n",
    "\n",
    "   ```sh\n",
    "   \"python.testing.pytestArgs\": [\n",
    "      \"python\", \"-s\"\n",
    "   ],\n",
    "   ```\n",
    "   * If you choose the option _Run Test_ you should see logs in the _Output Window_ --> _Python Test Log_\n",
    "   * If you choose the option _Debug Test_ you should see logs in the _DEBUG CONSOLE_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ad404c",
   "metadata": {},
   "source": [
    "<h2>Debugging Python and C++ in VS Code</h2>\n",
    "\n",
    "1. Set up debugging configurations for _Python_ and _C++ Attach_. As an example, the launch configuration file (`launch.json`) should contain\n",
    "\n",
    "    ```sh\n",
    "    \"configurations\": [\n",
    "        {\n",
    "            \"name\": \"(Windows) Attach\",\n",
    "            \"type\": \"cppvsdbg\",\n",
    "            \"request\": \"attach\",\n",
    "            \"processId\": \"${command:pickProcess}\"\n",
    "        },\n",
    "        {\n",
    "            \"name\": \"Python: Current File\",\n",
    "            \"type\": \"python\",\n",
    "            \"request\": \"launch\",\n",
    "            \"program\": \"${file}\",\n",
    "            \"console\": \"integratedTerminal\"\n",
    "        }\n",
    "    ]\n",
    "    ```\n",
    "\n",
    "2. For `interpret` we want to be sure that `ctypes` loads the debug build. One way to do this is to import the debug_mode script and use it in the script/test you are running\n",
    "\n",
    "    ```\n",
    "    from interpret.develop import debug_mode\n",
    "    debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)\n",
    "    ```\n",
    "\n",
    "3. Make sure the C++ project is compiled in DEBUG mode\n",
    "\n",
    "4. Start debugging a Python script / test\n",
    "\n",
    "5. Launch _C++ Attach_ and choose which PID to attach to\n",
    "\n",
    "   * It can be a bit hard to determine which is the correct PID to attach to. One way is to add the following Python code to your Python script to know its PID\n",
    "\n",
    "        ```\n",
    "        print('Current PID = {}'.format(os.getpid()))\n",
    "        ```\n",
    "\n",
    "6. At this point both Python and C++ Attach debuggers should be seen in the VS Code debugger toolbar"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
